package com.imooc.business.core.executor.query.base.table;

import com.imooc.business.core.executor.query.base.AbstractTableService;
import com.imooc.module.query.enums.OrganType;
import com.imooc.module.query.enums.TableType;
import com.imooc.module.query.request.QueryRequest;
import com.imooc.module.query.request.SupportType;
import com.imooc.module.query.response.QueryDataResponse;
import com.imooc.user.provider.model.GroupType;
import com.imooc.user.provider.model.UserCenterGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Tim
 * @description: 群组报表
 */
@Slf4j
@Repository
public class GroupTableService extends AbstractTableService<UserCenterGroupVo,String> {

    /**
     * 根据当前群组所在的学校id，获取当前学校所有的群组
     * @param request
     * @return
     */
    @Override
    protected List<UserCenterGroupVo> getOrganList(QueryRequest request) {
        UserCenterGroupVo group = request.getGroup();
        List<UserCenterGroupVo> groupVos = groupService.getGroupsBySchoolId(group.getParentId(), Arrays.asList(GroupType.CLASS.getType()));
        return groupVos;
    }

    @Override
    protected Map<Object, Map<String, Object>> getDataMap(QueryRequest request, List<UserCenterGroupVo> groupVos) {
        request.setGroupIds(groupVos.stream().map(UserCenterGroupVo::getGroupId).collect(Collectors.toList()));
        Map<Object, Map<String, Object>> dataMap = execQuery(request);
        return dataMap;
    }

    @Override
    protected void setSumaryAndClickAble(QueryDataResponse.TableData tableData, UserCenterGroupVo groupVo) {
        tableData.setIsSummary(false);
        tableData.setClickAble(GroupType.SCHOOL.equals(groupVo.getGroupType()) ? false : true);
    }

    @Override
    protected String getName(UserCenterGroupVo groupVo) {
        return groupVo.getGroupName();
    }

    @Override
    protected String getOrganId(UserCenterGroupVo groupVo) {
        return groupVo.getGroupId();
    }

    @Override
    public Boolean supportType(SupportType supportType) {
        return OrganType.GROUP.equals(supportType.getOrganType()) &&
                TableType.TABLE.equals(supportType.getTableType());
    }

}
